import pymysql
env_name = "test"
# env_name = "staging"

"""连接私有库"""
def new_get_data_conn(gitname):
    # 获取数据库连接
    if gitname =='customer':#连接私有库
        if env_name == "test":
            host1 = '10.8.62.13'
            port1 = 8003
            database = 'v2_pagoda_customer2'

        elif env_name == "staging":
            host1 = 'goods.ks1.pagoda.com.cn'
            port1 = 8003
            database = 'pagoda_customer2'
        else:
            # 默认的数据库连接信息
            host1 = '10.8.62.13'
            port1 = 8003
            database = 'v2_pagoda_customer2'

        dbconnect_coconfig = pymysql.connect(host=host1, port=port1, user='root', passwd='Pagoda@123!',db=database, charset='utf8')
        return dbconnect_coconfig


    else:
        if env_name == "test":
            host1 = '10.8.62.13'
            port1 = 8004
            database = 'v2_pagoda_overall'

        elif env_name == "staging":
            host1 = 'goods.ks1.pagoda.com.cn'
            port1 = 8002
            database = 'pagoda_overall'
        else:
            # 默认的数据库连接信息
            host1 = '10.8.62.13'
            port1 = 8004
            database = 'v2_pagoda_overall'

        dbconnect_coconfig = pymysql.connect(host=host1, port=port1, user='root', passwd='Pagoda@123!',db=database, charset='utf8')
        # 获取到连接
        return dbconnect_coconfig

# 获得指定sql的返回值
def new_get_one_date_from_sql(sql,Environmental_parameters):
    try:
        """获取到连接,输入customer之外的所有字段，都连接到overall库
            Environmental_parameters表明环境参数，输入Environmental_parameters=‘customer’ 则表明为私有库；否则为全局库
        """
        dbconnect_coconfig = new_get_data_conn(Environmental_parameters)
        mycursor = dbconnect_coconfig.cursor()#获取游标
        mycursor.execute(sql)#执行sql语句
        rownum=mycursor.rowcount#打印表里面数据的条数
        for one in range(rownum):#循环遍历打印每条数据
            rowdata=mycursor.fetchone()
            # print(rowdata)
        return rownum
    except Exception as e:
        print("Error: %s" % str(e))
    finally:
        if dbconnect_coconfig:
            dbconnect_coconfig.close()

# 删除指定sql的数据
def new_delete_history_data(deleteSql):
    dbconnect_coconfig = new_get_data_conn()
    mycursor = dbconnect_coconfig.cursor()
    data = mycursor.execute(deleteSql)
    dbconnect_coconfig.commit()
    print(data)

# 执行更新sql
def new_update_data(updateSql):
    dbconnect_coconfig = new_get_data_conn()
    mycursor = dbconnect_coconfig.cursor()
    data = mycursor.execute(updateSql)
    dbconnect_coconfig.commit()
    print(data)

if __name__ == '__main__':
    print(new_get_one_date_from_sql("select * from chains_order",'customer'))
    # print(new_get_one_date_from_sql("select * from article",'overall'))
    # new_update_data("update chains_order set orderAmount='1500' where id='4'")
    # new_delete_history_data("delete from chains_order where id='4'")